<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="榜单年份" prop="year" v-if="queryParams.listType == 0">
        <el-date-picker v-model="queryParams.year" type="year" placeholder="选择年" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:article:add']"
        >新增榜单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:article:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="toplistList" row-key="id" :tree-props="{children: 'children'}" @selection-change="handleSelectionChange">
      <el-table-column label="榜单名称" align="left" prop="rankingName" />
      <el-table-column label="创建日期" align="center" prop="createTime" width="160" />
      <el-table-column label="修改日期" align="center" prop="updateTime" width="160" />
      <el-table-column label="发布日期" align="center" prop="releaseTime" width="160" />
      <el-table-column label="操作" align="right" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            plain
            type="primary"
            icon="el-icon-edit"
            v-if="scope.row.children == null || scope.row.children.length == 0 "
            @click="handleUpdate(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            plain
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="榜单名称" prop="rankingName">
          <el-input v-model="form.rankingName" placeholder="请输入榜单名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryArchiveList as queryList, getItem, delItem, addItem, updateItem, getRelationLawyerList, releaseRankingList, archiveRankingList } from "@/api/mobile/toplist"
import router from "@/router"
export default {
  name: "ToplistArchive",
  dicts: ['business_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      toplistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      lawyerList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      imgAction: `${process.env.VUE_APP_BASE_API}/common/commonUpload`,
    }
  },
  created() {
    this.getList()
    getRelationLawyerList().then(response => {
      this.lawyerList = response.data
    })
  },
  methods: {
    handleRelease(row) {
      releaseRankingList(row.id).then(response => {
        this.$modal.msgSuccess("发布成功")
        this.getList()
      })
    },
    handleArchive(row) {
      archiveRankingList(row.id).then(response => {
        this.$modal.msgSuccess("归档成功")
        this.getList()
      })
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      queryList(this.queryParams).then(response => {
        this.toplistList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: 0,
        rankingName: ""
      }
      this.yearMonth = null
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(parentId = 0) {
      this.reset()
      this.open = true
      if(parentId == 0){
        this.title = "新增榜单"
      }else {
        this.title = "新增子类"
      }
      this.form.parentId = parentId
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset()
      router.push(`/mobile/topList/mobileTopListEdit?id=${row.id}&pageType=detail`);
      // const id = row.id || this.ids
      // getItem(id).then(response => {
      //   this.form = response.data
      //   if(response.data.listType == 1){
      //     this.yearMonth = response.data.year + '-' + response.data.month
      //   }
      //   this.open = true
      //   this.title = "修改信息"
      // })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除信息编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/toplist/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    },
    // 状态修改
    handleStatusChange(row) {
      let text = row.status == 0 ? "停用" : "启用"
      console.log(row);
      this.$modal.confirm(`确认要"${text}"${row.title}吗？`).then(function() {
        return updateItem({id :row.id, status: row.status})
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(function() {
        row.status = row.status == "0" ? 1 : 0
      })
    },
    beforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 5
      if (!isRightSize) {
        this.$message.error('文件大小超过 5MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
    uploadSuccess(res){
      this.form.img = res.path;
    }
  }
}
</script>

